import { defineStore } from "pinia";
import { computed, ComputedRef, reactive, Ref, ref } from "vue";
import { pageParams, userItem, userListInfo } from "@/api/permissions/userApi/interface.ts";
import { deleteUserAPI, fetchUserListAPI } from "@/api/permissions/userApi/userApi.ts";

const useUserListStore = defineStore("userListStore", () => {
  /* 分页的参数 */
  const pageParams: pageParams = reactive<pageParams>({ page: 1, limit: 10,username:undefined });

  /* 用户列表信息 */
  const userListInfo: Ref<userListInfo> = ref<userListInfo>({} as userListInfo);
  /* 用户列表 */
  const userList: ComputedRef<userItem[]> = computed<userItem[]>(() => userListInfo.value.records);
  /* 用户的总条数 */
  const total: ComputedRef<number> = computed<number>(() => userListInfo.value.total);
  /* 发送请求获取用户列表的回调函数 */
  const fetchUserList = async (): Promise<void> => {
    const { data }: { data: userListInfo } = await fetchUserListAPI(pageParams);
    userListInfo.value = data;
  };
  /* 发送请求删除用户的回调函数 */
  const deleteUser = async (id: number): Promise<string> => {
    await deleteUserAPI(id);
    return "删除用户成功";
  };
  return {
    pageParams,
    userListInfo,
    userList,
    total,
    fetchUserList,
    deleteUser,
  };
});

export default useUserListStore;
